<template>
  <!-- 页面-菜单管理-添加 -->
  <div class="sidelip-box">
    <Form
      :model="modelForm"
      ref="serviceForm"
      class="ivu-form-no-margin-bottom"
      :rules="rule"
      :label-width="95"
    >
      <Row :gutter="32" class="code-row-bg">
        <Col span="12" style="border:none;">
          <Form-item prop="code" :label="$t('module.serviceManage.servicecode')">
            <Input
              :placeholder="$t('module.serviceManage.enterservicename')"
              
              v-model.trim="modelForm.code"
              style="width:100%"
            ></Input>
          </Form-item>
        </Col>
        <Col span="12" style="border:none;">
          <Form-item prop="name" :label="$t('module.serviceManage.servicename')">
            <Input
              :placeholder="$t('module.serviceManage.enterservicecode')"
              
              v-model.trim="modelForm.name"
              :maxlength="50"
              style="width:100%"
            ></Input>
          </Form-item>
        </Col>
      </Row>
      <Row :gutter="32" class="code-row-bg">
        <Col span="12" style="border:none;">
          <Form-item prop="serviceAddress" :label="$t('module.serviceManage.serviceaddress')">
            <Input
              :placeholder="$t('module.serviceManage.enterserviceaddress')"
              
              v-model.trim="modelForm.serviceAddress"
              :maxlength="50"
              style="width:100%"
            ></Input>
          </Form-item>
        </Col>
        <Col span="12" style="border:none;">
          <Form-item prop="enabled" :label="$t('common.status') ">
            <RadioGroup v-model="modelForm.enabled" >
              <Radio :label="1">
                <span>{{ $t("common.enable") }}</span>
              </Radio>
              <Radio :label="0">
                <span>{{ $t("common.disable") }}</span>
              </Radio>
            </RadioGroup>
          </Form-item>
        </Col>
      </Row>
    </Form>
  </div>
</template>
<script>
import menuAPI from "@/api/menuAPI.js";
import locales from "./locales.js";
import { codeReg } from "@/const.js";
import { addServiceProvider, updateServiceProvider } from "@/api/serviceMange.js";


// 一天的毫秒数
const ONE_DAY = 86400000;
export default {
  name: "menu-edit",
  mixins: [locales],
  components: {},
  props: {
    tenantCode: {
      type: String,
      default() {
        return "";
      }
    },
    rowList: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      modelForm: {
        code: "",
        name: "",
        serviceAddress: "",
        enabled: 1
      },
      rule: {
        code: [
           {
            required: true,
            message: this.$t("module.serviceManage.enterservicename"),
            trigger: "blur"
          }
        ],
        name: [
          {
            required: true,
            message: this.$t("module.serviceManage.enterservicecode"),
            trigger: "blur"
          }
        ],
        serviceAddress: [
          {
            required: true,
            message: this.$t("module.serviceManage.enterserviceaddress"),
            trigger: "change"
          }
        ]
      },
      menuTypeList: [],
      actions: [
        {
          text: this.$t("common.save"),
          theme: "warning",
          icon: "edit",
          handle: () => {
            this.save();
          },
          loading: false
        },
        {
          text: this.$t("common.close"),
          theme: "primary",
          handle: () => {
            this.$emit("Close");
          }
        }
      ]
    };
  },
  computed: {},
  created: function() {
    if (this.rowList && this.rowList.id) {
      this.$emit("SetTitle", this.$t("module.serviceManage.editservice"));
    } else {
      this.$emit("SetTitle", this.$t("module.serviceManage.createservice"));
    }
    this.$emit("SetPageWidth", 700);
    this.$emit("SetPageActions", this.actions);
  },
  mounted: function() {
    this.init();
  },
  methods: {
    init() {
      if (this.rowList && this.rowList.id) {
        this.modelForm = Object.assign({}, this.rowList);
      }
    },
    save() {
      this.$refs["serviceForm"].validate(valid => {
        if (valid) {
          this.actions[0].loading = true;
          if (this.modelForm.id) {
            let param = this.modelForm;
            updateServiceProvider(param)
              .then(response => {
                this.actions[0].loading = false;
                this.$message.success({
                  content: this.$t("module.serviceManage.warning.updateSuccessfully"),
                  duration: 3
                });
                this.$emit("Close", true);
              })
              .catch(data => {
                this.actions[0].loading = false;
              });
          } else {
            let param = this.modelForm;
            addServiceProvider(param)
              .then(response => {
                this.actions[0].loading = false;
                this.$message.success({
                  content: this.$t("module.serviceManage.warning.createMenuSuccessfully"),
                  duration: 3
                });
                this.$emit("Close", true);
              })
              .catch(data => {
                this.actions[0].loading = false;
              });
          }
        }
      });
    }
  }
};
</script>
<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  padding-right: 8px;
}
.ivu-form .ivu-form-item-label {
  padding: 10px 9px 10px 0 !important;
}
</style>
